package com.example.mediaandroidjava;

import androidx.appcompat.app.AppCompatActivity;
import androidx.media3.datasource.DataSource;
import androidx.media3.exoplayer.ExoPlayer;
import androidx.media3.exoplayer.source.DefaultMediaSourceFactory;
import androidx.media3.exoplayer.source.MediaSource;
import androidx.media3.exoplayer.source.ads.AdsLoader;
import androidx.media3.ui.PlayerView;

import android.content.Context;
import android.os.Bundle;

public class MediaSourceActivity extends AppCompatActivity {
    private PlayerView playerView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_media_source);
        playerView = findViewById(R.id.media_source_player);
    }

    ///自定义创建媒体源
    private void customizingMediaSourceCreation(Context context, PlayerView playerView, DataSource.Factory cacheDataSourceFactory, AdsLoader.Provider adsLoaderProvider) {
        MediaSource.Factory mediaSourceFactory = new DefaultMediaSourceFactory(context)
                .setDataSourceFactory(cacheDataSourceFactory)
                .setLocalAdInsertionComponents(adsLoaderProvider, /* adViewProvider= */ playerView);
        ExoPlayer player = new ExoPlayer.Builder(context)
                .setMediaSourceFactory(mediaSourceFactory)
                .build();
    }

    ///初始化列表
    private void mediaSourceBasedPlaylist(Context context, PlayerView playerView, DataSource.Factory cacheDataSourceFactory, AdsLoader.Provider adsLoaderProvider) {
        MediaSource.Factory mediaSourceFactory = new DefaultMediaSourceFactory(context)
                .setDataSourceFactory(cacheDataSourceFactory)
                .setLocalAdInsertionComponents(adsLoaderProvider, /* adViewProvider= */ playerView);
        ExoPlayer player = new ExoPlayer.Builder(context)
                .setMediaSourceFactory(mediaSourceFactory)
                .build();
    }
}